package com.beasu.tingting.services

import android.app.Service
import android.content.Intent
import android.media.AudioAttributes
import android.media.AudioFormat
import android.media.AudioTrack
import android.os.Binder
import android.os.IBinder

class SingleService : Service() {
    private val mBinder = SingleBinder()
    private  lateinit var  track :AudioTrack
    var frequency = 44100

    override fun onBind(intent: Intent): IBinder {
            return  mBinder
    }

    override fun onDestroy() {
        super.onDestroy()
        track.stop()
        track.release()

    }
    override fun onCreate() {
        super.onCreate()
        track = AudioTrack.Builder()
            .setAudioFormat(AudioFormat.Builder()
                .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
                .setSampleRate(44100)
                .setChannelMask(AudioFormat.CHANNEL_OUT_DEFAULT)
                .build())
            //       .setBufferSizeInBytes(wave.size)
            .build()
        track.play()
    }
    inner  class SingleBinder : Binder() {
            fun setFrequency(frequency :Int){
                this@SingleService.frequency = frequency
            }
            fun play(wave :ShortArray){
              //  track.play()
                track.write(wave,0,wave.size)

            }

    }
}
